'use strict';

const conn = require('../../libs/DB/Conn');
const moment = require('moment');
const _ = require("underscore");
module.exports = async function (params) {
    try {
        let startTime = moment(params.startTime).format("YYYY-MM-DD");
        let endTime = moment(params.endTime).format("YYYY-MM-DD");
        let querySql = `SELECT
                     rf.range_name AS rangeName,
                     DATE_FORMAT(dd.data_date,"%Y-%m-%d") AS dataDate,
                     DATE_FORMAT(dd.last_data_time,"%Y-%m-%d %H:%i:%s") AS lastDataTime,
                     df.device_id AS deviceId,
                     df.device_no AS deviceNo,
                     df.device_name AS deviceName,
                     dd.date_accumulated AS dateAccumulated,
                     dd.date_consumption AS dateConsumption,
                     dd.date_cost_amount AS dateCostAmount,
                     dd.sharp_consumption AS sharpConsumption,
                     dd.sharp_cost_amount AS sharpCostAmount,
                     dd.peak_consumption AS peakConsumption,
                     dd.peak_cost_amount AS peakCostAmount,
                     dd.flat_consumption AS flatConsumption,
                     dd.flat_cost_amount AS flatCostAmount,
                     dd.valley_consumption AS valleyConsumption,
                     dd.valley_cost_amount AS valleyCostAmount,
                     dd.other_consumption AS otherConsumption,
                     dd.other_cost_amount AS otherCostAmount,
                     dd.cycle_consumption AS cycleConsumption,
                     cpf.cost_plan_name AS costPlanName,
                     cpf.cost_plan_type AS costPlanType,
                     cpf.measure_type AS measureType
                 FROM 
                    daily_data_${params.userDomainId} dd
                 INNER JOIN cost_plan_file cpf ON cpf.cost_plan_id = dd.cost_plan_id AND cpf.measure_type = ?
                 INNER JOIN device_file df ON dd.device_id = df.device_id
                 AND df.delete_sign = 1 
                 AND df.range_id IN (?)
                 INNER JOIN range_file rf ON rf.range_id = df.range_id AND rf.delete_sign = 1
                 WHERE
                 dd.data_date BETWEEN ? AND ?
                 ORDER BY data_date DESC`
        let historyCostList = await conn(querySql,[params.rangeIds,params.measureType,startTime,endTime]);
        let historyDeviceGroup = _.groupBy(historyCostList,"deviceId");
        let dataList = [];
        for(const deviceId in historyDeviceGroup){
            let deviceList = historyDeviceGroup[deviceId];
            let deviceObj = {
                deviceId:deviceList[0].deviceId,
                deviceNo:deviceList[0].deviceNo,
                dataDate:deviceList[0].dataDate,
                dateAccumulated:deviceList[0].dateAccumulated,
                dateConsumption:deviceList[0].dateConsumption,
                dateCostAmount:deviceList[0].dateCostAmount,
                historyList:[],
            };
            for(const index in deviceList){
                if(!deviceList[index].lastDataTime){
                    deviceList[index].dateAccumulated = "-";
                    deviceList[index].dateConsumption = "-";
                    deviceList[index].dateCostAmount  = "-";
                    deviceList[index].sharpConsumption= "-";
                    deviceList[index].sharpCostAmount = "-";
                    deviceList[index].peakConsumption = "-";
                    deviceList[index].peakCostAmount  = "-";
                    deviceList[index].flatConsumption = "-";
                    deviceList[index].flatCostAmount  = "-";
                    deviceList[index].valleyConsumption= "-";
                    deviceList[index].valleyCostAmount= "-";
                    deviceList[index].otherConsumption= "-";
                    deviceList[index].otherCostAmount = "-";
                    deviceList[index].cycleConsumption= "-";
                }
            }
            deviceObj.historyList = deviceList;
            dataList.push(deviceObj);
        }
        return {success: true,dataList:dataList};
    }catch (e){
        return {success:false,message:e.message}
    }
};